package com.xing.fragment;

import java.util.ArrayList;
import java.util.List;
import com.xing.adapter.ManageListNativeAdapter;
import com.xing.adapter.ManageListServerAdapter;
import com.xing.controller.CommonController;
import com.xing.controller.DBController;
import com.xing.model.HsdModelSurvey;
import com.xing.model.HsdModelSurveyListObj;
import com.xing.model.HsdQuestionAndAnswerModelListObj;
import com.xing.my_interface.CallBack;
import com.xing.questionnairesurvey.R;
import com.xing.util.CurrentUserUtil;
import com.xing.util.ViewUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ManageFragment extends SuperFragment implements CallBack{
	
	private View view;
	private FragmentManager fragmentManager;
	private ListView listView;
	private Button leftButton;
	private Button rightButton;
	
	private List<HsdModelSurvey> nativeList;
	private ManageListServerAdapter serverAdapter;
	private ManageListNativeAdapter nativeAdapter;
	
	private HsdQuestionAndAnswerModelListObj hsdQuestionModelListObj ;
	
	private List<HsdModelSurvey> currentList = new ArrayList<HsdModelSurvey>();
	
	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		fragmentManager = getFragmentManager();
		return (view = inflater.inflate(R.layout.manager_fragment,container,false));
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		userNameTextView = (TextView) view.findViewById(R.id.user_name);
		userNameTextView.setText(CurrentUserUtil.userName+",欢迎使用!");
		leftButton = (Button) view.findViewById(R.id.left_btn);
		rightButton = (Button) view.findViewById(R.id.right_btn);
		leftButton.setOnClickListener(myListener);
		rightButton.setOnClickListener(myListener);
		listView = (ListView) view.findViewById(R.id.listview);
		
		
	   DBController.getInstance().getHsdModelSurveyuuidList(getActivity(),surveyuuidsListHandler);
	   
	   
	}
	
	private Handler surveyuuidsListHandler = new Handler(){
		public void handleMessage(Message msg) {
			List<String> list = (List<String>) msg.obj;
			StringBuffer sb = new StringBuffer();
			for (String string : list) {
				sb.append(string);
				sb.append(",");
			}
			
			 String token = ""; 
			 String username="";
			 if (TextUtils.isEmpty(sb.toString())) {
				 CommonController.getInstance().getHsdModelSurveyListObj("",username, token,handler1, getActivity());;
			}else {
				CommonController.getInstance().getHsdModelSurveyListObj(sb.toString(),username, token,handler1, getActivity());
			}
			
		}
	};
	private Handler handler1 = new Handler(){
		public void handleMessage(Message msg) {
			
			currentList.clear();
		    HsdModelSurveyListObj hsdModelSurveyListObj = (HsdModelSurveyListObj) msg.obj;
		    
			if (hsdModelSurveyListObj.status == 1) {
				currentList.addAll(hsdModelSurveyListObj.undownload);
				currentList.addAll(hsdModelSurveyListObj.download);
//				currentList.addAll(hsdModelSurveyListObj.passtime);
//				currentList.addAll(hsdModelSurveyListObj.deleted);
				if (serverAdapter == null) {
					serverAdapter = new ManageListServerAdapter(getActivity(), currentList,fragmentManager,ManageFragment.this);
				}
				listView.setAdapter(serverAdapter);
				serverAdapter.notifyDataSetChanged();
				/**
				 * 问卷状态
				 * 1 not download
				 * 2:has downloaded
				 * 3:out of date
				 * 4:has deleted
				 */
				for (HsdModelSurvey hsdModelSurvey : hsdModelSurveyListObj.passtime) {
					DBController.getInstance().updateHsdModelSurveyStatus(getActivity(), hsdModelSurvey.getSurveyuuid(), 3, outOfdateStatusHandler);
				}
				
				for (HsdModelSurvey hsdModelSurvey : hsdModelSurveyListObj.deleted) {
					DBController.getInstance().updateHsdModelSurveyStatus(getActivity(), hsdModelSurvey.getSurveyuuid(), 4, deletedStatusHandler);
				}
			
				
			}else {
				ViewUtil.showToast(getActivity(), hsdModelSurveyListObj.msg);
			}
		}
	};
	
	private Handler deletedStatusHandler =  new Handler(){
		public void handleMessage(android.os.Message msg) {
			boolean isUpdate = (Boolean) msg.obj;
			if(isUpdate){
				
			}else {
				/**
				 * 失败后应该重新重新插入
				 */
				ViewUtil.showToast(getActivity(), "更新本地问卷状态失败");
				
			}
			
		}
	};
	
	private Handler outOfdateStatusHandler =  new Handler(){
		public void handleMessage(android.os.Message msg) {
			boolean isUpdate = (Boolean) msg.obj;
			if(isUpdate){
				
			}else {
				/**
				 * 失败后应该重新重新插入
				 */
				ViewUtil.showToast(getActivity(), "更新本地问卷状态失败");
				
			}
			
		}
	};

	
	
	private Handler getNativeHsdModelSurveyListHandler =  new Handler(){
		public void handleMessage(android.os.Message msg) {
			List<HsdModelSurvey> nativeList = (List<HsdModelSurvey>) msg.obj;
			if (nativeAdapter == null) {
				nativeAdapter = new ManageListNativeAdapter(getActivity(), nativeList,fragmentManager);
			}
			listView.setAdapter(nativeAdapter);
			nativeAdapter.notifyDataSetChanged();
		}
	};
	
	private OnClickListener myListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.left_btn:
				DBController.getInstance().getHsdModelSurveyuuidList(getActivity(),surveyuuidsListHandler);
				leftButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.table_left_bg_on));
				rightButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.table_right_bg_default));
				break;
			case R.id.right_btn:
				DBController.getInstance().getHsdModelSurveyList(getActivity(), getNativeHsdModelSurveyListHandler);
				leftButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.table_left_bg_default));
				rightButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.table_right_bg_on));
				break;
			default:
				break;
			}
		}
	};



	@Override
	public void fresh() {
		// TODO Auto-generated method stub
		 DBController.getInstance().getHsdModelSurveyuuidList(getActivity(),surveyuuidsListHandler);
	}

}

